package com.laf.manager.controller;

import com.laf.manager.controller.dto.MemberCarDto;
import com.laf.manager.controller.dto.PointsDeductionDto;
import com.laf.manager.querycondition.DeductionQueryCondition;
import com.laf.manager.querycondition.MemberCarQueryCondition;
import com.laf.manager.service.MemberCarService;
import com.laf.manager.service.PointsDeductionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ygl
 * @create 2019-01-04
 * @DESCRIPTION
 **/
@Controller
@Slf4j
public class MemberCarController {

    @Autowired
    private MemberCarService memberCarService;


    @GetMapping("/memberCar")
    public ModelAndView get(HttpSession session) {
        MemberCarQueryCondition condition = null;

        if (session.getAttribute("memberCar_queryCondition") != null) {
            condition = (MemberCarQueryCondition) session.getAttribute("memberCar_queryCondition");
        } else {
            condition = new MemberCarQueryCondition();
        }

        List<MemberCarDto> list = memberCarService.getList(condition);

        int total = memberCarService.getCountList(condition);

        ModelAndView model = new ModelAndView();

        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("index", condition.getPage());
        pageMap.put("size", condition.getSize());
        pageMap.put("total", total);

        model.getModel().put("pageMap", pageMap);
        model.getModel().put("list", list);
        model.setViewName("memberCar");

        return model;
    }

    @PostMapping("/memberCar/filter")
    public String filterActivities(MemberCarQueryCondition condition, BindingResult errors, HttpSession session) {
        session.setAttribute("memberCar_queryCondition", condition);

        return "redirect:/memberCar";
    }

    @GetMapping("/memberCar/unbind/{id}")
    public String unbind(@PathVariable int id) {
        memberCarService.deleteById(id);
        return "redirect:/memberCar";
    }
}
